<template>


    <el-table :data="review_items" :style="{ width: '100%' }">
        <el-table-column prop="achievement_type_name" label="成果类型" width="180" />
        <el-table-column prop="achievement_name" label="成果名" width="700" />
        <el-table-column prop="created_date" label="创建时间" />
        <el-table-column fixed="right" label="操作" width="300">
            <template #default="scope">
                <el-row>
                    <el-button type="primary" @click="pass(scope.row)" size="small">通过审核</el-button>
                    <el-button type="danger" @click="refuse(scope.row)" size="small">拒绝通过</el-button>
                </el-row>
            </template>
        </el-table-column>
    </el-table>


    <el-dialog v-model="showCommentDiglog" title="拒绝理由">

        <el-input v-model="refuseReason" type="textarea" :rows="20" placeholder="请输入内容" />

        <template #footer>
            <span class="dialog-footer">
                <el-button @click="showCommentDiglog=!showCommentDiglog">取消</el-button>
                <el-button type="primary" @click="refuseReview"> 拒绝通过 </el-button>
            </span>
        </template>
    </el-dialog>


</template>



<script setup>


import http from '@/utils/axios.js'
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'



const currentItem = reactive({})

// 通过审核
const pass = (row) => {
    currentItem.value = { ...row };
    console.log(currentItem);
    
    try {
        http.patch('/api/review/pass', {
            achievementId: currentItem.value.achievement_id
        })
        .then(res => {
            console.log(res.data);
            if (res.data.code === 4000) {

                ElMessage.success('通过审核成功')
                review_items.length = 0
                getReviews()
                
            } else {
                ElMessage.error('通过审核失败')
            }
        })
        
    } catch (error) {
        console.log(error);
    }

}


//  拒绝通过审核
const showCommentDiglog = ref(false)
const refuseReason = ref('')
const refuse = (row) => {
    currentItem.value = { ...row };
    showCommentDiglog.value = true
    console.log(currentItem);
}

const refuseReview = () => {
    try {
        http.patch('/api/review/refuse', {
            achievementId: currentItem.value.achievement_id,
            reason: refuseReason.value
        })
        .then(res => {
            console.log(res.data);
            if (res.data.code === 4000) {
                showCommentDiglog.value = true
                ElMessage.success('审核拒绝成功')

                review_items.length = 0
                getReviews()
            } else {
                ElMessage.error('审核拒绝失败')
            }
        })

    } catch (error) {
        
    }

}


const review_items = reactive([])
// 从后端获取初始化数据
onMounted(() => {
    try {
        console.log("初始化数据")
        getReviews()

    } catch (error) {
        console.error('Error fetching student academic exchange info:', error);
    }
})

const getReviews = () => {
    http.get('/api/review').then(res => {
        Object.assign(review_items, res.data)
        console.log(res.data);
    })
}




</script>